/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import path from 'path';
import { PluginFileUtil } from '../../framework';
import { FilePathConstants, RouterMapConstants, TemplateConstants } from '../constants';

export class HMRouterPluginConfig {
  moduleName: string;
  modulePath: string;
  configDir: string;
  scanDir: string[];
  routerMapDir: string;
  builderDir: string;
  defaultPageTemplate: string;
  customPageTemplate: CustomPageTemplateImpl[];
  saveGeneratedFile: boolean;
  autoObfuscation: boolean;

  constructor(moduleName: string, modulePath: string, configDir: string, param: HMRouterPluginConfigParam) {
    this.moduleName = moduleName;
    this.modulePath = modulePath;
    this.configDir = configDir;
    this.scanDir = param.scanDir ? [...new Set(param.scanDir)] : [];
    this.routerMapDir = param.routerMapDir || '';
    this.builderDir = param.builderDir || '';
    this.defaultPageTemplate = param.defaultPageTemplate || '';
    this.customPageTemplate = param.customPageTemplate || [];
    this.saveGeneratedFile = !!param.saveGeneratedFile;
    this.autoObfuscation = !!param.autoObfuscation;
  }

  /**
   * @description Obtain scanned path
   * @param dir
   * @return string Scanned path
   */
  getScanPath(dir: string):string {
    return PluginFileUtil.pathResolve(this.modulePath, dir);
  }

  /**
   * @description Obtain relative source path
   * @param filePath
   * @return string, relative source path
   */
  getRelativeSourcePath(filePath: string):string {
    return path.relative(this.modulePath, filePath);
  }

  /**
   * @description Get the relative path of the Builder
   * @param filePath
   * @return string, the relative path of the Builder
   */
  getRelativeBuilderPath(filePath: string):string {
    return path.relative(this.builderDir, filePath);
  }

  /**
   * @description Get the absolute path of the generated file
   * @return string, Absolute path of the generated component
   * @param generatorViewName
   */
  getGeneratedFilePath(generatorViewName: string):string {
    return PluginFileUtil.pathResolve(
      this.modulePath,
      this.builderDir,
      generatorViewName + FilePathConstants.ETS_SUFFIX
    );
  }

  /**
   * @description Get the absolute path of the generation directory
   * @return string, Absolute path of builderDir
   */
  getBuilderDir():string {
    return PluginFileUtil.pathResolve(this.modulePath, this.builderDir);
  }

  getBuilderFilePath(generatorViewName: string):string {
    return path.join(this.builderDir, generatorViewName + FilePathConstants.ETS_SUFFIX);
  }

  getRouterMapDir():string {
    return PluginFileUtil.pathResolve(this.modulePath, this.routerMapDir, RouterMapConstants.ROUTER_MAP_NAME);
  }

  getModuleRouterMapFilePath(routerMapFileName: string):string {
    return PluginFileUtil.pathResolve(
      this.modulePath,
      this.routerMapDir,
      routerMapFileName + FilePathConstants.JSON_SUFFIX
    );
  }

  getRawFilePath():string {
    return PluginFileUtil.pathResolve(this.modulePath, FilePathConstants.RAWFILE_DIR);
  }

  getDefaultTplFilePath():string {
    if (path.isAbsolute(this.defaultPageTemplate) && PluginFileUtil.exist(this.defaultPageTemplate)) {
      return this.defaultPageTemplate;
    }
    return PluginFileUtil.pathResolve(__dirname, FilePathConstants.PARENT_DELIMITER.repeat(3) + TemplateConstants.VIEW_BUILDER_TEMPLATE);
  }

  getObfuscationFilePath():string {
    return PluginFileUtil.pathResolve(this.modulePath, FilePathConstants.OBFUSCATION_FILE_NAME);
  }

  getConsumerRulesFilePath():string {
    return PluginFileUtil.pathResolve(this.modulePath, FilePathConstants.CONSUMER_FILE_NAME);
  }
}

export interface HMRouterPluginConfigParam {
  scanDir?: string[];
  routerMapDir?: string;
  builderDir?: string;
  autoObfuscation?: boolean;
  saveGeneratedFile?: boolean;
  defaultPageTemplate?: string;
  customPageTemplate?: CustomPageTemplateImpl[];
}

export interface CustomPageTemplateImpl {
  srcPath: string[];
  templatePath: string;
}
